%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% File:               representatives_game.m
%
% Author:             Miguel R. Rueda and Sergio Ascencio
%
% Description:        Computes structural parameters with two step ML
% estimator
% Language:           MATLAB R2013b (8.2.0.701) 64 Bit
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [betas,hessian,phats,LL,auxLL,Llong]=representatives_game(Y,X,state,CLv_PAN,CLv_PRI,beta_p)

%First stage results
if state==1
    [phats,~]=first_stage(Y,X,CLv_PAN,CLv_PRI);
else
    [phats,~]=first_stage(Y,X);
end

%Second stage results
if nargin<6
    [beta,LL,hessian,auxLL,Llong]=Params2(Y,X,phats);
    betas=reshape(beta,[length(beta)/4,4]);
else
    [beta,LL,hessian,auxLL,Llong]=Params2(Y,X,phats,beta_p);
    betas=reshape(beta,[length(beta)/4,4]);
end

end